home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CDsupport / aMiPEG / source / decoders.h < prev    next >
C/C++ Source or Header  |  1994-04-22  |  13KB  |  393 lines

  1. /*
  2.  * decoders.h
  3.  *
  4.  * This file contains the declarations of structures required for Huffman
  5.  * decoding
  6.  *
  7.  */
  8.  
  9. /* Include util.h for bit i/o parsing macros. */
  10.  
  11. #include "util.h"
  12.  
  13. /* Code for unbound values in decoding tables */
  14. #define ERROR -1
  15. #define DCT_ERROR 63
  16.  
  17. #define MACRO_BLOCK_STUFFING 34
  18. #define MACRO_BLOCK_ESCAPE 35
  19.  
  20. /* Two types of DCT Coefficients */
  21. #define DCT_COEFF_FIRST 0
  22. #define DCT_COEFF_NEXT 1
  23.  
  24. /* Special values for DCT Coefficients */
  25. #define END_OF_BLOCK 62
  26. #define ESCAPE 61
  27.  
  28. /* Structure for an entry in the decoding table of 
  29.  * macroblock_address_increment */
  30. typedef struct {
  31.   char value;            /* value for macroblock_address_increment (was unsigned !) */
  32.   char num_bits;        /* length of the Huffman code */
  33. } mb_addr_inc_entry;
  34.  
  35. /* Decoding table for macroblock_address_increment */
  36. extern mb_addr_inc_entry mb_addr_inc[2048];
  37.  
  38.  
  39. /* Structure for an entry in the decoding table of macroblock_type */
  40. typedef struct {
  41.   char data;            /* quant motion_forward motion_backward pattern intra */
  42.   char num_bits;        /* length of the Huffman code */
  43. } mb_type_entry;
  44.  
  45. #define MB_quant    8
  46. #define MB_motion_forw    4
  47. #define MB_motion_back    2
  48. #define MB_pattern    1
  49.  
  50.  
  51. /* Decoding table for macroblock_type in intra-coded pictures */
  52. extern mb_type_entry mb_type_I[4];
  53.  
  54. /* Decoding table for macroblock_type in predictive-coded pictures */
  55. extern mb_type_entry mb_type_P[64];
  56.  
  57. /* Decoding table for macroblock_type in bidirectionally-coded pictures */
  58. extern mb_type_entry mb_type_B[64];
  59.  
  60.  
  61. /* Structure for an entry in the decoding table of motion vectors */
  62. typedef struct {
  63.   char code;            /* value for motion_horizontal_forward_code,
  64.                  * motion_vertical_forward_code, 
  65.                  * motion_horizontal_backward_code, or
  66.                  * motion_vertical_backward_code.
  67.                  */
  68.   char num_bits;        /* length of the Huffman code */
  69. } motion_vectors_entry;
  70.  
  71. /* Decoding table for motion vectors */
  72. extern motion_vectors_entry motion_vectors[2048];
  73.  
  74.  
  75. /* DCT coeff tables. */
  76.  
  77. #define RUN_MASK 0xfc00
  78. #define LEVEL_MASK 0x03f0
  79. #define NUM_MASK 0x000f
  80. #define RUN_SHIFT 10
  81. #define LEVEL_SHIFT 4
  82.  
  83. /* External declaration of dct coeff tables. */
  84.  
  85. extern unsigned short int dct_coeff_tbl_0[256];
  86. extern unsigned short int dct_coeff_tbl_1[16];
  87. extern unsigned short int dct_coeff_tbl_2[4];
  88. extern unsigned short int dct_coeff_tbl_3[4];
  89. extern unsigned short int dct_coeff_next[256];
  90. extern unsigned short int dct_coeff_first[256];
  91.  
  92. /*
  93.  *--------------------------------------------------------------
  94.  *
  95.  * DecodeDCTDCSizeLum --
  96.  *
  97.  *    Huffman Decoder for dct_dc_size_luminance; location where
  98.  *      the result of decoding will be placed is passed as argument.
  99.  *      The decoded values are obtained by doing a table lookup on
  100.  *      dct_dc_size_luminance.
  101.  *
  102.  * Results:
  103.  *    The decoded value for dct_dc_size_luminance or ERROR for 
  104.  *      unbound values will be placed in the location specified.
  105.  *
  106.  * Side effects:
  107.  *    Bit stream is irreversibly parsed.
  108.  *
  109.  *--------------------------------------------------------------
  110.  */
  111. #define DecodeDCTDCSizeLum(macro_val)    macro_val = s_DecodeDCTDCSizeLum()
  112.  
  113. /*
  114.  *--------------------------------------------------------------
  115.  *
  116.  * DecodeDCTDCSizeChrom --
  117.  *
  118.  *    Huffman Decoder for dct_dc_size_chrominance; location where
  119.  *      the result of decoding will be placed is passed as argument.
  120.  *      The decoded values are obtained by doing a table lookup on
  121.  *      dct_dc_size_chrominance.
  122.  *
  123.  * Results:
  124.  *    The decoded value for dct_dc_size_chrominance or ERROR for
  125.  *      unbound values will be placed in the location specified.
  126.  *
  127.  * Side effects:
  128.  *    Bit stream is irreversibly parsed.
  129.  *
  130.  *--------------------------------------------------------------
  131.  */
  132. #define DecodeDCTDCSizeChrom(macro_val)    macro_val = s_DecodeDCTDCSizeChrom()
  133.  
  134.  
  135. /*
  136.  *--------------------------------------------------------------
  137.  *
  138.  * decodeDCTCoeff --
  139.  *
  140.  *    Huffman Decoder for dct_coeff_first and dct_coeff_next;
  141.  *      locations where the results of decoding: run and level, are to
  142.  *      be placed and also the type of DCT coefficients, either
  143.  *      dct_coeff_first or dct_coeff_next, are being passed as argument.
  144.  *      
  145.  *      The decoder first examines the next 8 bits in the input stream,
  146.  *      and perform according to the following cases:
  147.  *      
  148.  *      '0000 0000' - examine 8 more bits (i.e. 16 bits total) and
  149.  *                    perform a table lookup on dct_coeff_tbl_0.
  150.  *                    One more bit is then examined to determine the sign
  151.  *                    of level.
  152.  *
  153.  *      '0000 0001' - examine 4 more bits (i.e. 12 bits total) and 
  154.  *                    perform a table lookup on dct_coeff_tbl_1.
  155.  *                    One more bit is then examined to determine the sign
  156.  *                    of level.
  157.  *      
  158.  *      '0000 0010' - examine 2 more bits (i.e. 10 bits total) and
  159.  *                    perform a table lookup on dct_coeff_tbl_2.
  160.  *                    One more bit is then examined to determine the sign
  161.  *                    of level.
  162.  *
  163.  *      '0000 0011' - examine 2 more bits (i.e. 10 bits total) and 
  164.  *                    perform a table lookup on dct_coeff_tbl_3.
  165.  *                    One more bit is then examined to determine the sign
  166.  *                    of level.
  167.  *
  168.  *      otherwise   - perform a table lookup on dct_coeff_tbl. If the
  169.  *                    value of run is not ESCAPE, extract one more bit
  170.  *                    to determine the sign of level; otherwise 6 more
  171.  *                    bits will be extracted to obtain the actual value 
  172.  *                    of run , and then 8 or 16 bits to get the value of level.
  173.  *                    
  174.  *      
  175.  *
  176.  * Results:
  177.  *    The decoded values of run and level or ERROR for unbound values
  178.  *      are placed in the locations specified.
  179.  *
  180.  * Side effects:
  181.  *    Bit stream is irreversibly parsed.
  182.  *
  183.  *--------------------------------------------------------------
  184.  */
  185. #define DecodeDCTCoeff(dct_coeff_tbl, run, level)    run = s_DecodeDCTCoeff(dct_coeff_tbl, &level)
  186.  
  187. /*
  188.  *--------------------------------------------------------------
  189.  *
  190.  * decodeDCTCoeffFirst --
  191.  *
  192.  *    Huffman Decoder for dct_coeff_first. Locations for the
  193.  *      decoded results: run and level, are being passed as
  194.  *      arguments. Actual work is being done by calling DecodeDCTCoeff,
  195.  *      with the table dct_coeff_first.
  196.  *
  197.  * Results:
  198.  *    The decoded values of run and level for dct_coeff_first or
  199.  *      ERROR for unbound values are placed in the locations given.
  200.  *
  201.  * Side effects:
  202.  *    Bit stream is irreversibly parsed.
  203.  *
  204.  *--------------------------------------------------------------
  205.  */
  206. #define DecodeDCTCoeffFirst(runval, levelval)    DecodeDCTCoeff(dct_coeff_first, runval, levelval)
  207.  
  208. /*
  209.  *--------------------------------------------------------------
  210.  *
  211.  * decodeDCTCoeffNext --
  212.  *
  213.  *    Huffman Decoder for dct_coeff_first. Locations for the
  214.  *      decoded results: run and level, are being passed as
  215.  *      arguments. Actual work is being done by calling DecodeDCTCoeff,
  216.  *      with the table dct_coeff_next.
  217.  *
  218.  * Results:
  219.  *    The decoded values of run and level for dct_coeff_next or
  220.  *      ERROR for unbound values are placed in the locations given.
  221.  *
  222.  * Side effects:
  223.  *    Bit stream is irreversibly parsed.
  224.  *
  225.  *--------------------------------------------------------------
  226.  */ 
  227. #define DecodeDCTCoeffNext(runval, levelval)    DecodeDCTCoeff(dct_coeff_next, runval, levelval);
  228.  
  229.  
  230. /*
  231.  *--------------------------------------------------------------
  232.  *
  233.  * DecodeMotionVectors --
  234.  *
  235.  *      Huffman Decoder for the various motion vectors, including
  236.  *      motion_horizontal_forward_code, motion_vertical_forward_code,
  237.  *      motion_horizontal_backward_code, motion_vertical_backward_code.
  238.  *      Location where the decoded result will be placed is being passed
  239.  *      as argument. The decoded values are obtained by doing a table
  240.  *      lookup on motion_vectors.
  241.  *
  242.  * Results:
  243.  *      The decoded value for the motion vector or ERROR for unbound
  244.  *      values will be placed in the location specified.
  245.  *
  246.  * Side effects:
  247.  *      Bit stream is irreversibly parsed.
  248.  *
  249.  *--------------------------------------------------------------
  250.  */
  251. #ifdef NO_SANITY_CHECKS
  252. #define DecodeMotionVectors(value)    value = sn_get_byte_huff(11, (short*)motion_vectors)
  253. #else
  254. #define DecodeMotionVectors(value)    value = s_get_byte_huff(11, (short*)motion_vectors)
  255. #endif
  256.  
  257. /*
  258.  *--------------------------------------------------------------
  259.  *
  260.  * DecodeMBAddrInc --
  261.  *
  262.  *      Huffman Decoder for macro_block_address_increment; the location
  263.  *      in which the result will be placed is being passed as argument.
  264.  *      The decoded value is obtained by doing a table lookup on
  265.  *      mb_addr_inc.
  266.  *
  267.  * Results:
  268.  *      The decoded value for macro_block_address_increment or ERROR
  269.  *      for unbound values will be placed in the location specified.
  270.  *
  271.  * Side effects:
  272.  *      Bit stream is irreversibly parsed.
  273.  *
  274.  *--------------------------------------------------------------
  275.  */
  276. #ifdef NO_SANITY_CHECKS
  277. #define DecodeMBAddrInc(value)    value = sn_get_byte_huff(11, (short*)mb_addr_inc)
  278. #else
  279. #define DecodeMBAddrInc(value)    value = s_get_byte_huff(11, (short*)mb_addr_inc)
  280. #endif
  281.  
  282. /*
  283.  *--------------------------------------------------------------
  284.  *
  285.  * DecodeMBTypeB --
  286.  *
  287.  *      Huffman Decoder for macro_block_type in bidirectionally-coded
  288.  *      pictures;locations in which the decoded results: macroblock_quant,
  289.  *      macroblock_motion_forward, macro_block_motion_backward,
  290.  *      macroblock_pattern, macro_block_intra, will be placed are
  291.  *      being passed as argument. The decoded values are obtained by
  292.  *      doing a table lookup on mb_type_B.
  293.  *
  294.  * Results:
  295.  *      The various decoded values for macro_block_type in
  296.  *      bidirectionally-coded pictures or ERROR for unbound values will
  297.  *      be placed in the locations specified.
  298.  *
  299.  * Side effects:
  300.  *      Bit stream is irreversibly parsed.
  301.  *
  302.  *--------------------------------------------------------------
  303.  */
  304. #ifdef NO_SANITY_CHECKS
  305. #define DecodeMBTypeB(data, intra)    data=sn_get_bits_huff(6, &intra, (short*)mb_type_B)
  306. #else
  307. #define DecodeMBTypeB(data, intra)    data=s_get_bits_huff(6, &intra, (short*)mb_type_B)
  308. #endif
  309.  
  310. /*
  311.  *--------------------------------------------------------------
  312.  *
  313.  * DecodeMBTypeI --
  314.  *
  315.  *      Huffman Decoder for macro_block_type in intra-coded pictures;
  316.  *      locations in which the decoded results: macroblock_quant,
  317.  *      macroblock_motion_forward, macro_block_motion_backward,
  318.  *      macroblock_pattern, macro_block_intra, will be placed are
  319.  *      being passed as argument.
  320.  *
  321.  * Results:
  322.  *      The various decoded values for macro_block_type in intra-coded
  323.  *      pictures or ERROR for unbound values will be placed in the
  324.  *      locations specified.
  325.  *
  326.  * Side effects:
  327.  *      Bit stream is irreversibly parsed.
  328.  *
  329.  *--------------------------------------------------------------
  330.  */
  331.  
  332. #ifdef NO_SANITY_CHECKS
  333. #define DecodeMBTypeI(data, intra)    data=sn_get_bits_huff(2, &intra, (short*)mb_type_I)
  334. #else
  335. #define DecodeMBTypeI(data, intra)    data=s_get_bits_huff(2, &intra, (short*)mb_type_I)
  336. #endif
  337.  
  338.  
  339. /*
  340.  *--------------------------------------------------------------
  341.  *
  342.  * DecodeMBTypeP --
  343.  *
  344.  *      Huffman Decoder for macro_block_type in predictive-coded pictures;
  345.  *      locations in which the decoded results: macroblock_quant,
  346.  *      macroblock_motion_forward, macro_block_motion_backward,
  347.  *      macroblock_pattern, macro_block_intra, will be placed are
  348.  *      being passed as argument. The decoded values are obtained by
  349.  *      doing a table lookup on mb_type_P.
  350.  *
  351.  * Results:
  352.  *      The various decoded values for macro_block_type in
  353.  *      predictive-coded pictures or ERROR for unbound values will be
  354.  *      placed in the locations specified.
  355.  *
  356.  * Side effects:
  357.  *      Bit stream is irreversibly parsed.
  358.  *
  359.  *--------------------------------------------------------------
  360.  */
  361. #ifdef NO_SANITY_CHECKS
  362. #define DecodeMBTypeP(data, intra)    data=sn_get_bits_huff(6,&intra, (short*)mb_type_P)
  363. #else
  364. #define DecodeMBTypeP(data, intra)    data=s_get_bits_huff(6,&intra, (short*)mb_type_P)
  365. #endif
  366.  
  367.  
  368. /*
  369.  *--------------------------------------------------------------
  370.  *
  371.  * DecodeCBP --
  372.  *
  373.  *      Huffman Decoder for coded_block_pattern; location in which the
  374.  *      decoded result will be placed is being passed as argument. The
  375.  *      decoded values are obtained by doing a table lookup on
  376.  *      coded_block_pattern.
  377.  *
  378.  * Results:
  379.  *      The decoded value for coded_block_pattern or ERROR for unbound
  380.  *      values will be placed in the location specified.
  381.  *
  382.  * Side effects:
  383.  *      Bit stream is irreversibly parsed.
  384.  *
  385.  *--------------------------------------------------------------
  386.  */
  387. #ifdef NO_SANITY_CHECKS
  388. #define DecodeCBP(coded_bp)    coded_bp = sn_DecodeCBP()
  389. #else
  390. #define DecodeCBP(coded_bp)    coded_bp = s_DecodeCBP()
  391. #endif
  392.  
  393.